package com.cooperate.fly.controller;

import com.cooperate.fly.bo.Case;
import com.cooperate.fly.bo.CaseWithBLOBs;
import com.cooperate.fly.bo.Field;
import com.cooperate.fly.bo.User;
import com.cooperate.fly.mapper.CaseMapper;
import com.cooperate.fly.mapper.FieldMapper;
import com.cooperate.fly.mapper.TableMapper;
import com.cooperate.fly.service.system.Constant;
import com.cooperate.fly.web.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by baobao on 2016/5/9.
 */
@Controller
@RequestMapping("/table")
public class TableController extends BaseController {
    @Autowired
    TableMapper tableMapper;
    @Autowired
    CaseMapper caseMapper;
    @Autowired
    FieldMapper fieldMapper;

    @Value("#{propertiesReader['field_data_float_type']}")
    public int fieldFloatType;

    @RequestMapping(value="/update", method= RequestMethod.POST)
    @ResponseBody
    public Result update(HttpSession session, HttpServletRequest request){
        Result res = new Result();
        int caseId = Integer.parseInt(request.getParameter("caseId"));
        int fieldId = Integer.parseInt(request.getParameter("fieldId"));
        String value = request.getParameter("value");
        Case _case = caseMapper.selectByPrimaryKey(caseId);
        tableMapper.updateValue("table_"+_case.getSchemaId(), value, fieldId, caseId);
        //更新case_definition修改时间及修改人
        User user=(User) session.getAttribute(Constant.USER_SESSION_KEY);
        CaseWithBLOBs caseWithBLOBs = new CaseWithBLOBs();
        caseWithBLOBs.init(_case);
        caseWithBLOBs.setReviser(user.getUserName());
        caseMapper.updateTime(caseWithBLOBs);
        res.setMessage(value + "     " + fieldMapper.selectByPrimaryKey(fieldId).getRemark3());
        //System.out.println(res.getMessage());
        res.setSuccessful(true);
        return res;
    }

    @RequestMapping(value="/updateRemark", method= RequestMethod.POST)
    @ResponseBody
    public Result updateRemark(HttpSession session, HttpServletRequest request){
        Result res = new Result();
        int caseId = Integer.parseInt(request.getParameter("caseId"));
        int fieldId = Integer.parseInt(request.getParameter("fieldId"));
        String value = request.getParameter("remark");
        Case _case = caseMapper.selectByPrimaryKey(caseId);
        tableMapper.updateRemark("table_"+_case.getSchemaId(), value, fieldId, caseId);
        //更新case_definition修改时间及修改人
        User user=(User) session.getAttribute(Constant.USER_SESSION_KEY);
        CaseWithBLOBs caseWithBLOBs = new CaseWithBLOBs();
        caseWithBLOBs.init(_case);
        caseWithBLOBs.setReviser(user.getUserName());
        caseMapper.updateTime(caseWithBLOBs);
        res.setSuccessful(true);
        return res;
    }

    @RequestMapping(value="/getValue", method= RequestMethod.POST)
    @ResponseBody
    public Result getValue(HttpServletRequest request){
        Result res = new Result();
        int caseId = Integer.parseInt(request.getParameter("caseId"));
        int fieldId = Integer.parseInt(request.getParameter("fieldId"));
        Case _case = caseMapper.selectByPrimaryKey(caseId);
        String value = tableMapper.selectByCaseIdAndFieldId("table_"+_case.getSchemaId(), caseId, fieldId).getValue();
        res.setMessage(value);
        res.setSuccessful(true);
        return res;
    }

    @RequestMapping(value="/getFloatUnit", method = RequestMethod.POST)
    @ResponseBody
    public Result getFloatUnit(HttpServletRequest request){
        Result res = new Result();
        int caseId = Integer.parseInt(request.getParameter("caseId"));
        Case _case = caseMapper.selectByPrimaryKey(caseId);
        List<Field> fields = fieldMapper.selectBySchemaId(_case.getSchemaId());
        StringBuilder s = new StringBuilder();
        for(Field field : fields){
            if(field.getType()==fieldFloatType){
                s.append(field.getId() + "," + field.getRemark3() + ";");
            }
        }
        s.deleteCharAt(s.length()-1);
        res.setMessage(s.toString());
        //System.out.println(res.getMessage());
        res.setSuccessful(true);
        return res;
    }

    @RequestMapping(value="/getMatrixHead", method= RequestMethod.POST)
    @ResponseBody
    public Result getMatrixHead(HttpServletRequest request){
        Result res = new Result();
        int fieldId = Integer.parseInt(request.getParameter("fieldId"));
        Field field = fieldMapper.selectByPrimaryKey(fieldId);
        String head = field.getRemark3();
        res.setMessage(head);
        res.setSuccessful(true);
        return res;
    }
}
